﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using FlanProjectData.Managers;
using FlanProjectData.Helpers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace FlanProject.ScreenMenu
{
    class LoadingScreen : InterfaceScreen
    {
        private static Logger logger = new Logger("LoadingScreen");
        private String dossier = @"Saves\"; // chemin où se trouve les dossiers de sauvegarde
        DirectoryInfo dir;
        DirectoryInfo[] listeDossier = null;

        private float distanceBetween2Button = (float)0.12;	//Define the distance in % of 2 button

        private float widthDistanceButton = (float)0.36;	//Define the distance in % from the right-side & the button
        private float heightDistanceButton = (float)0.24;	//Define the distance in % from the top-side & the FIRST button

        private float heightPercentButton = (float)0.09;	//Define the height in % of the button
        private float widthPercentButton = (float)0.43;		//Define the width in % of the button

        private float widthDistanceTextUp = (float)0.04;   //Define the width position in % of the title ("Loading game")
        private float heightDistanceTextUp = (float)0.14;   //Define the height position in % of the title ("Loading game")

        private int policeSize = 1;

        #region Ctor
        public LoadingScreen()
        {
            int i = 0;

            dir = new DirectoryInfo(@"Content\"+dossier);
            listeDossier = dir.GetDirectories();
            logger.Debug("nom du repertoire: "+dir.FullName);
            logger.Debug("nbr repertoire: " + listeDossier.Length);

            for (int y = 0; y < listeDossier.Length; y++)
            {
                this.lstMenuEntrySelectable.Add(new MenuEntry(listeDossier.ElementAt < DirectoryInfo>(y).Name,
                new Vector2(((float)windowsWidth * widthDistanceButton),
                    ((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
                new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
            }
            i++;
            this.lstMenuEntrySelectable.Add(new MenuEntry("Retour",
                new Vector2(((float)windowsWidth * widthDistanceButton),
                    ((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
                new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
            this.lstMenuEntrySelectable[i-2].Selected += this.ExitScreen;


            this.lstMenuEntryUnselectable.Add(new MenuEntry("Load Game",
                new Vector2((float)windowsWidth * widthDistanceTextUp,
                    (float)windowsHeight * heightDistanceTextUp),
                    true, Color.Black, policeSize));

        }
        #endregion

        #region Overrided LoadContent
        public override void LoadContent(ContentManager content)
        {
            logger.Debug("on est dans le load content");
            this.background = content.Load<Texture2D>(@"Sprites\Menus\commonBackgroundMenu");

            // calculate the scale of the background
            base.calcScaleBackground(windowsHeight, windowsWidth);

            // Use virtual definition
            base.LoadContent(content);

        }
        #region Overrided HandleInput
        public override void HandleInput()
        {
            if (InputManager.IsActionTriggered(InputManager.Action.Exit))
            {
                logger.Warn("Fermeture du programme");
                this.ExitScreen();
            }
            else if (InputManager.IsActionTriggered(InputManager.Action.Ok))
            {
                logger.Info("Validation on :" + selectedIndex.ToString());
				
				// Load game only if saves exist (more than one entry on the screen)
                if (selectedIndex >= 0 && selectedIndex < 3 && this.lstMenuEntrySelectable.Count>1) 
                {
                    logger.Debug("fichier charger: " + listeDossier.ElementAt<DirectoryInfo>(selectedIndex).Name);
                    this.filesName = listeDossier.ElementAt<DirectoryInfo>(selectedIndex).Name;
                    this.IsLoadingGame = true;
                }
            }
            base.HandleInput();
        }
        #endregion

        #endregion 

    }
}
